﻿using System.Text.RegularExpressions;

namespace System.Web.Mvc
{
    public static class StringExtensions
    {
        public static string TurkishSeo(this string str)
        {
            string originalValue = str.ToLower();
            str = str.Replace('ö', 'o');
            str = str.Replace('ü', 'u');
            str = str.Replace('ğ', 'g');
            str = str.Replace('ş', 's');
            str = str.Replace('ı', 'i');
            str = str.Replace('Ö', 'o');
            str = str.Replace('Ü', 'u');
            str = str.Replace('Ğ', 'g');
            str = str.Replace('Ş', 's');
            str = str.Replace('İ', 'i');
            str = Regex.Replace(str, "[^a-z^0-9]", "-", RegexOptions.IgnoreCase);
            string pattern = "--";
            while (Regex.IsMatch(str, pattern))
            {
                str = Regex.Replace(str, pattern, "-", RegexOptions.IgnoreCase);
            }
            pattern = "^-|-$";
            str = Regex.Replace(str, pattern, "", RegexOptions.IgnoreCase);
            return str.ToLower();
        }

        public static string UriCombine(this string val, string append)
        {
            if (String.IsNullOrEmpty(val))
            {
                return append;
            }

            if (String.IsNullOrEmpty(append))
            {
                return val;
            }

            return val.TrimEnd('/') + "/" + append.TrimStart('/');
        }
    }
}